APP_NAME = test_decoder

## Chip & board used for compilation
## (can be overriden by adding CHIP=chip and BOARD=board to the command-line)
CHIP  = at91sam9263
BOARD = at91sam9263-ek

## Trace level used for compilation
## (can be overriden by adding TRACE_LEVEL=#number to the command-line)
#define TRACE_LEVEL_DEBUG      5
#define TRACE_LEVEL_INFO       4
#define TRACE_LEVEL_WARNING    3
#define TRACE_LEVEL_ERROR      2
#define TRACE_LEVEL_FATAL      1
#define TRACE_LEVEL_NO_TRACE   0
TRACE_LEVEL = 0

## Optimization level, put in comment for debugging
#OPTIMIZATION = -O2

## Root directory
ROOT = ../..

## D264 library directory
D264 = $(ROOT)/d264
D264OBJS = $(D264)/ldecod/obj/*.o

## External library
EXT_LIBS = $(ROOT)/ext

## Output file basename
OUTPUT = $(APP_NAME)-$(BOARD)-$(CHIP)

## Directories where source files can be found
AT91LIB = $(EXT_LIBS)/at91lib
  BOARDS = $(AT91LIB)/boards
  COMPONENTS = $(AT91LIB)/components
  DRIVERS = $(AT91LIB)/drivers
  PERIPHERALS = $(AT91LIB)/peripherals
  MEM = $(AT91LIB)/memories
  UTILITY = $(AT91LIB)/utility
FATFS = $(EXT_LIBS)/fatfs

## Compile for all memories available on the board (this sets $(MEMORIES))
include $(AT91LIB)/boards/$(BOARD)/board.mak

## Output directories
BIN = bin
OBJ = obj


################################################################################
## TOOLS

## Tool suffix when cross-compiling
CROSS_COMPILE = arm-none-eabi-

## Compilation tools
CC = $(CROSS_COMPILE)gcc
SIZE = $(CROSS_COMPILE)size
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy

## Includes
INCLUDES =  -I.
INCLUDES += -I$(MENU)
INCLUDES += -I$(VIEWER)
INCLUDES += -I$(AT91LIB)
INCLUDES += -I$(BOARDS)/$(BOARD)
INCLUDES += -I$(COMPONENTS)
INCLUDES += -I$(DRIVERS)
INCLUDES += -I$(MEM)
INCLUDES += -I$(PERIPHERALS)
INCLUDES += -I$(FATFS)

INCLUDES += -I$(UTILITY)

INCLUDES += -I$(D264) -I$(D264)/ldecod/inc -I$(D264)/lcommon/inc

## Flags
CFLAGS = -Wall -mlong-calls -ffunction-sections
CFLAGS += -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -DTRACE_LEVEL=$(TRACE_LEVEL)
ASFLAGS = -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -D__ASSEMBLY__
LDFLAGS = -g $(OPTIMIZATION) -nostartfiles -Wl,--gc-sections


################################################################################
## FILES

VPATH += $(BOARDS)/$(BOARD)
VPATH += $(BOARDS)/$(BOARD)/$(CHIP)
VPATH += $(COMPONENTS)/ads7843
VPATH += $(DRIVERS)/lcd
VPATH += $(DRIVERS)/tsd
VPATH += $(DRIVERS)/pit
VPATH += $(PERIPHERALS)/aic
VPATH += $(PERIPHERALS)/cp15
VPATH += $(PERIPHERALS)/pmc
VPATH += $(PERIPHERALS)/dbgu
VPATH += $(PERIPHERALS)/lcd
VPATH += $(PERIPHERALS)/pio
VPATH += $(PERIPHERALS)/pit
VPATH += $(PERIPHERALS)/spi
VPATH += $(PERIPHERALS)/tsadcc
VPATH += $(UTILITY)
VPATH += $(FATFS)

VPATH += $(MEM)
VPATH += $(PERIPHERALS)/mci
VPATH += $(MEM)/sdmmc

## Objects built from C source files
C_OBJECTS =  main.o
C_OBJECTS += minsys.o
C_OBJECTS += board_memories.o
C_OBJECTS += board_lowlevel.o
C_OBJECTS += ads7843.o
C_OBJECTS += draw.o
C_OBJECTS += font.o
C_OBJECTS += lcdd.o
C_OBJECTS += clock.o
C_OBJECTS += pitd.o
C_OBJECTS += tsd_com.o
C_OBJECTS += tsd_ads7843.o
C_OBJECTS += tsd_tsadc.o
C_OBJECTS += aic.o
C_OBJECTS += pmc.o
C_OBJECTS += dbgu.o
C_OBJECTS += lcd.o
C_OBJECTS += pio.o
C_OBJECTS += pio_it.o
C_OBJECTS += pit.o
C_OBJECTS += spi.o
C_OBJECTS += tsadcc.o
C_OBJECTS += string.o
C_OBJECTS += stdio.o
C_OBJECTS += cp15.o

C_OBJECTS += Media.o MEDSdcard.o
C_OBJECTS += math.o util.o
C_OBJECTS += diskio.o ff.o
C_OBJECTS += sdmmc_mci.o mci.o

C_OBJECTS += bmp.o

## Objects built from Assembly source files
ASM_OBJECTS = board_cstartup.o
ASM_OBJECTS += cp15_asm.o

## Append OBJ and BIN directories to filenames
OUTPUT := $(BIN)/$(OUTPUT)


################################################################################
## RULES

all: d264lib $(BIN) $(OBJ) dep $(MEMORIES)

d264lib:
	make -C $(D264)

dependencies: $(addprefix dep/, $(addsuffix .d, $(MEMORIES)))

$(BIN) $(OBJ) dep:
	mkdir $@

define RULES
C_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(C_OBJECTS))
ASM_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(ASM_OBJECTS))
C_DEPS_$(1) = $(addprefix dep/$(1)_, $(patsubst %.o, %.d, $(C_OBJECTS)))
ASM_DEPS_$(1) = $(addprefix dep/$(1)_, $(patsubst %.o, %.d, $(ASM_OBJECTS)))

dep/$(1).d: $$(C_DEPS_$(1)) $$(ASM_DEPS_$(1))
	cat $$^ > $$@
	echo $(1): $(AT91LIB)/boards/$(BOARD)/$(CHIP)/$(1).lds >> $$@
	rm -f $$^

$$(C_DEPS_$(1)): dep/$(1)_%.d: %.c Makefile dep
	$(CC) $(CFLAGS) -MM -D$(1) $$< > $$@

$$(ASM_DEPS_$(1)): dep/$(1)_%.d: %.S Makefile dep
	$(CC) $(ASFLAGS) -MM -D$(1) $$< > $$@

$(1): $$(ASM_OBJECTS_$(1)) $$(C_OBJECTS_$(1))
	@echo
	$(CC) $(LDFLAGS) -T"$(AT91LIB)/boards/$(BOARD)/$(CHIP)/$$@.lds" -o $(OUTPUT)-$$@.elf $$^ $(D264OBJS)
	$(OBJCOPY) -O binary $(OUTPUT)-$$@.elf $(OUTPUT)-$$@.bin
	$(SIZE) $$^ $(OUTPUT)-$$@.elf

$$(C_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.c Makefile
	$(CC) $(CFLAGS) -D$(1) -c -o $$@ $$<

$$(ASM_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.S Makefile
	$(CC) $(ASFLAGS) -D$(1) -c -o $$@ $$<

endef

$(foreach MEMORY, $(MEMORIES), $(eval $(call RULES,$(MEMORY))))

clean:
	-rm -f $(OBJ)/*.o $(BIN)/*.bin $(BIN)/*.elf
	make -C $(D264) $@

debug: all
	arm-none-eabi-gdbtui $(OUTPUT)-sram.elf -x $(ROOT)/config/at91sam9263_sram.gdb

